package com.hst.capacity.facade.rest;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.hst.capacity.application.biz.FranchiseeBiz;
import com.hst.capacity.application.biz.RegionBiz;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.setting.station.ApplyStationEntity;
import com.hst.capacity.domain.model.entity.setting.station.StationEntity;
import com.hst.capacity.domain.model.request.franchisee.FranchiseeParam;
import com.hst.capacity.domain.model.request.franchisee.commision.FranchiseeCommisionParam;
import com.hst.capacity.domain.model.request.franchisee.commision.FranchiseeCommisionSaveParam;
import com.hst.capacity.domain.model.response.DefaultVO;
import com.hst.capacity.domain.model.response.franchisee.FranchiseRegionCodeVO;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hst.capacity.domain.model.response.DefaultVO.success;


/**
 *  运营商模块Controller，包括运营商 城市信息, CRUD，检索等。
 *
 **/
@RequestMapping("/franchisee")
@RestController
public class FranchiseeController {

    @Autowired
    private RegionBiz regionBiz;

    @Autowired
    private FranchiseeBiz franchiseeBiz;

    /**
     * 获取运营商运营城市Code
     * @param franParam
     * @return
     */
    @PostMapping("/region/code")
    private DefaultVO getRegionCode(@RequestBody @Valid FranchiseeParam franParam){
        List<FranchiseRegionCodeVO> entity = regionBiz.findRegionCodeByIdFranchisee(franParam.getIdFranchisee());
        if (entity == null)
            throw new BusinessException(4001, "运营商未找到");;
        return success(entity);
    }

    /**
     * 获取城市的市内车站列表
     * @param franParam
     * @return
     */
    @PostMapping("/region/station/list")
    private DefaultVO getStationLst(@RequestBody @Valid FranchiseeParam franParam){
        List<StationEntity> list = null;
        if (franParam.getStationBizType()==0){
            list = regionBiz.getInnerCityStationByCode(franParam.getStationRegionCode(),franParam.getRegionCode());
        }
        if (franParam.getStationBizType()==1){
            list = regionBiz.getInterCityStationByCode(franParam.getStationRegionCode(),franParam.getRegionCode());
        }
        return success(list);
    }

    /**
     * 获取城市的市内车站列表
     * @param franParam
     * @return
     */
    @PostMapping("/region/custom/station/list")
    private DefaultVO getCustomStationLst(@RequestBody @Valid FranchiseeParam franParam){
        List<StationEntity> list = null;
        if (franParam.getStationBizType()==0){
            list = regionBiz.getInnerCityStationByCode(franParam.getStationRegionCode(),franParam.getRegionCode());
        }
        if (franParam.getStationBizType()==1){
            list = regionBiz.getInterCityStationByCode(franParam.getStationRegionCode(),franParam.getRegionCode());
        }
        return success(list);
    }

    /**
     * 获取城市的跨城车站列表
     * @param franParam
     * @return
     */
    @PostMapping("/region/intercity/station/list")
    private DefaultVO getIntercityStationLst(@RequestBody @Valid FranchiseeParam franParam){
        return success(regionBiz.getInnercityStationByCode(franParam.getRegionCode()));
    }

    /**
     * 获取城市的机场列表
     * @param franParam
     * @return
     */
    @PostMapping("/region/airport/list")
    private DefaultVO getAirportLst(@RequestBody @Valid FranchiseeParam franParam){
        return success(regionBiz.getAirportByCityCode(franParam.getRegionCode()));
    }

    /**
     * 获取城市的火车站列表
     * @param franParam
     * @return
     */
    @PostMapping("/region/railStation/list")
    private DefaultVO getRailStationLst(@RequestBody @Valid FranchiseeParam franParam){
        return success(regionBiz.getRailStationByCityCode(franParam.getRegionCode()));
    }

    /**
     * 运营商资料
     * @param param
     * @return
     */
    @PostMapping("/select")
    private DefaultVO select(@RequestBody Map<String,String> param){
        String idFranchisee = param.get("idFranchisee");
        if (StrUtil.isEmpty(idFranchisee))
            throw new BusinessException(4001,"运营商ID[idFranchisee]不能为空");
        return success(regionBiz.select(idFranchisee));
    }

//    /**
//     * 保存运营商
//     * @param param
//     * @return
//     */
//    @PostMapping("/save")
//    private DefaultVO save(@RequestBody @Valid FranchiseeSaveParam param){
//        return success(MapUtil.builder("idFranchisee",franchiseeBiz.save(param)).build());
//    }

    /**
     * 运营商分成比例展示
     * @param param
     * @return
     */
    @PostMapping("/commision/select")
    private DefaultVO franchiseeCommisionSelect(@RequestBody @Valid FranchiseeCommisionParam param){
        HashMap<String,Object> result = new HashMap<>();
        result.put("list", franchiseeBiz.findFranCommisionByIdFranchisee(param.getIdFranchisee()));
        return success(result);
    }

    /**
     * 运营商分成比例展示
     * @param param
     * @return
     */
    @PostMapping("/commision/detail")
    private DefaultVO franchiseeCommisionDetail(@RequestBody @Valid FranchiseeCommisionParam param){
        if (StringUtil.isBlank(param.getIdFranchiseeCommision()))
            throw new BusinessException(4001,"主键ID[idFranchiseeCommision]不能为空");
        return success(franchiseeBiz.findFranCommision(param));
    }

    /**
     * 保存运营商分成比例
     * @param param
     * @return
     */
    @PostMapping("/commision/save")
    private DefaultVO commisionSave(@RequestBody @Valid FranchiseeCommisionSaveParam param){
        return success(MapUtil.builder("idFranchiseeCommision",franchiseeBiz.commisionSave(param)).build());
    }

    /**
     * 该运力商下服务商数量
     * @param param
     * @return
     */
    @PostMapping("/agent/count")
    private DefaultVO agentCount(@RequestBody Map<String,String> param){
        String idFranchisee = param.get("idFranchisee");
        if (StrUtil.isEmpty(idFranchisee))
            throw new BusinessException(4001,"运营商ID[idFranchisee]不能为空");
        return success(franchiseeBiz.agentCount(idFranchisee));
    }

    /**
     * 根据城市编码获取城市信息
     * @param
     * @return
     */
    @PostMapping("/region/cityCode/list")
    private DefaultVO getRegionListByCityCode(@RequestBody FranchiseeParam franParam){
        String regionCode = franParam.getRegionCode();
        if (StrUtil.isEmpty(regionCode))
            throw new BusinessException(4001,"地区行政编码[regionCode]不能为空");
        return success(regionBiz.findRegionByCityCode(franParam.getRegionCode()));
    }

    /**
     * 根据城市编码获取城市信息
     * @param
     * @return
     */
    @PostMapping("/daily/stat")
    private DefaultVO dailyStat(@RequestBody FranchiseeParam franParam){
        String idFranchisee = franParam.getIdFranchisee();
        if (StrUtil.isEmpty(idFranchisee))
            throw new BusinessException(4001,"地区行政编码[regionCode]不能为空");
        return success(franchiseeBiz.dailyStat(franParam.getIdFranchisee()));
    }
    
}
